package com.fishingwithme.android.ui.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.zIndex
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.fishingwithme.android.R
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.navigation.FishingNavigation.ADD_SPOT
import com.fishingwithme.android.ui.theme.AppDimensions.ICON_SIZE_24
import com.fishingwithme.android.ui.theme.AppDimensions.ICON_SIZE_48

// Bottom navigation items matching WeChat Mini Program's tabBar
sealed class BottomNavItem(
    val route: String,
    val icon: Int,
    val label: String,
    val isEdit: Boolean = false
) {

    object AnyBitting :
        BottomNavItem(FishingNavigation.ANYBITTING, R.mipmap.anybiting_active, "有口么")

    object Activities :
        BottomNavItem(FishingNavigation.ACTIVITIES, R.mipmap.activity_active, "活动")

    object AddSpot : BottomNavItem(ADD_SPOT, R.mipmap.new_spot, "", true)
    object Spots : BottomNavItem(FishingNavigation.SPOTS, R.mipmap.home_active, "钓点库")
    object My : BottomNavItem(FishingNavigation.MY, R.mipmap.my_active, "我的")
}

@Composable
fun BottomNavigationBar(
    navController: NavController,
    content: @Composable () -> Unit
) {
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route

    val bottomNavItems = listOf(
        BottomNavItem.AnyBitting,
        BottomNavItem.Spots,
        BottomNavItem.AddSpot,
        BottomNavItem.Activities,
        BottomNavItem.My
    )

    // Routes where bottom navigation should be visible
    val showBottomNav = bottomNavItems.any { it.route == currentRoute }

    // State for EditDrawer visibility
    val isEditDrawerVisible = remember { mutableStateOf(false) }

    Scaffold(
        bottomBar = {
            if (showBottomNav) {
                Box(modifier = Modifier.zIndex(Float.MAX_VALUE)) {
                    NavigationBar {
                        bottomNavItems.forEach { item ->
                            NavigationBarItem(
                                icon = {
                                    Icon(
                                        painter = painterResource(item.icon),
                                        contentDescription = item.label,
                                        modifier = Modifier.size(if (item.isEdit) ICON_SIZE_48 else ICON_SIZE_24)
                                    )
                                },
                                label = {
                                    if (item.label.isNotEmpty()) {
                                        Text(item.label)
                                    }
                                },
                                selected = currentRoute == item.route,
                                colors = NavigationBarItemDefaults.colors(
                                    selectedIconColor = MaterialTheme.colorScheme.primary,
                                    selectedTextColor = MaterialTheme.colorScheme.primary,
                                    unselectedIconColor = MaterialTheme.colorScheme.onSurfaceVariant,
                                    unselectedTextColor = MaterialTheme.colorScheme.onSurfaceVariant,
                                    indicatorColor = Color.Transparent
                                ),
                                onClick = {
                                    if (item.isEdit) {
                                        // Show the edit drawer
                                        isEditDrawerVisible.value = true
                                    } else if (currentRoute != item.route) {
                                        navController.navigate(item.route)
                                    }
                                }
                            )
                        }
                    }
                }
            }
        }
    ) { paddingValues ->
        Box(modifier = Modifier.padding(paddingValues)) {
            content()

            // Edit Drawer component
            EditDrawer(
                isVisible = isEditDrawerVisible.value,
                onDismiss = { isEditDrawerVisible.value = false },
                navController = navController
            )
        }
    }
}